package com.qiaofang.housewarehouse.base.util;


import java.util.ArrayList;
import java.util.List;

public class ArraySelfUtils {

    public static void main(String[] args) {
        String str = "13817329875,13817329875,17821608204,15000875816,17302167915,17349774627,17717051920,13817329875,17821608204,15000875816,17302167915,17349774627,17717051920,13817329875";
        int splitSize = 6;//分割的块大小
        Object[] subAry = splitAry(str.split(","), splitSize);//分割后的子块数组
        for(Object obj: subAry){//打印输出结果
            String[] aryItem = (String[]) obj;
            for(int i = 0; i < aryItem.length; i++){
                System.out.print(aryItem[i] + ", ");
            }
            System.out.println();
        }
    }
    /**
     * splitAry方法<br>
     * @param ary 要分割的数组
     * @param subSize 分割的块大小
     * @return
     *
     */
    public static Object[] splitAry(String[] ary, int subSize) {
        int count = ary.length % subSize == 0 ? ary.length / subSize: ary.length / subSize + 1;

        List<List<String>> subAryList = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            int index = i * subSize;
            List<String> list = new ArrayList<>();
            int j = 0;
            while (j < subSize && index < ary.length) {
                list.add(ary[index++]);
                j++;
            }
            subAryList.add(list);
        }

        Object[] subAry = new Object[subAryList.size()];

        for(int i = 0; i < subAryList.size(); i++){
            List<String> subList = subAryList.get(i);
            String[] subAryItem = new String[subList.size()];
            for(int j = 0; j < subList.size(); j++){
                subAryItem[j] = subList.get(j);
            }
            subAry[i] = subAryItem;
        }

        return subAry;
    }
}
